package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo5 {
    public static void main(String[] args) {
        /*
        查看王克晶所带班级的信息？
        列出老师名字，工资，职称以及班级名字的信息
         */
        try (
                Connection connection = DBUtil.getConnection();
                ){
            Statement statement = connection.createStatement();
            String sql = "SELECT t.name,t.salary,t.title,c.name,c.floor " +
                    "FROM teacher t " +
                    "JOIN class c ON t.id = c.teacher_id " +
                    "WHERE t.name = '王克晶'";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
            //获取字段值时可以使用字段索引，
            // 注意：从1开始为结果集第一个字段，
            // 可读性差，不推荐
                String tName = rs.getString(1);
                int tSalary = rs.getInt(2);
                String tTitle = rs.getString(3);
                String CName = rs.getString(4);
                int cFloor = rs.getInt(5);
                System.out.println(tName+" "+tSalary+" "+tTitle+" "+CName+" "+cFloor);
            }
            } catch (SQLException e){
            e.printStackTrace();
        }
        }
    }

